package com.xy.mainp.novel.fragment.home.ui;

import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;

import com.xy.mainp.base.BasePActivity;
import com.xy.mainp.model.biqu.home.BiquHomeHotEntity;
import com.xy.mainp.model.biqu.home.classify.ClassfiyBookEntity;
import com.xy.mainp.model.biqu.home.newbook.LastInBookEntity;
import com.xy.mainp.model.biqu.home.newbook.LastUpdateEntity;
import com.xy.mainp.model.biqu.home.novellist.NovelListEntity;
import com.xy.mainp.novel.fragment.home.NovelHomeFragment;
import com.xy.mainp.novel.widget.NovelFragmentHelper;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by John on 2016/12/5.
 */

public class NovelHomePresenter implements NovelHomeCanstract.NovelHomePresenterImpl, NovelFragmentHelper.NetDataListener {

    private final NovelHomeModel model;
    private List<BiquHomeHotEntity> hotEntities;
    private ClassfiyBookEntity classfiyBook;
    private ArrayList<NovelListEntity> novelLists;
    private LastUpdateEntity lastUpdate;
    private LastInBookEntity lastIn;
    private MyHandler handler;
    private static Fragment fragment;
    private static final int HOT_DATA = 1;
    private static final int CLASSFIY_DATA = 2;
    private static final int BOOK_LIST = 3;

    public NovelHomePresenter(NovelHomeFragment fragment) {
        this.fragment = fragment;
        model = new NovelHomeModel();
        NovelFragmentHelper.getInstance(this.fragment.getActivity()).setNetDateListener(this);
        handler = new MyHandler((BasePActivity) this.fragment.getActivity());
    }

    @Override
    public void getData(String url) {
        NovelFragmentHelper.getInstance(fragment.getActivity()).LoadNetData(url);
    }

    @Override
    public void deal(final String result) {  //这个在子线程中

        Document document = Jsoup.parse(result);
        hotEntities = model.getHotBooks(document);
        if (hotEntities != null) {
            handler.obtainMessage(HOT_DATA, hotEntities).sendToTarget();
        }
        //经典推荐
        classfiyBook = model.getClassifiyBooks(document);
        if (classfiyBook != null) {
            handler.obtainMessage(CLASSFIY_DATA, classfiyBook).sendToTarget();
        }
        //分类列表
        novelLists = new ArrayList<>();
        Elements content = document.select("div.content");
        for (int j = 0; j < content.size(); j++) {
            Element s = content.get(j);
            NovelListEntity list = model.getNovelListEntity(s);
            novelLists.add(list);
        }
        if (novelLists != null) {
            handler.obtainMessage(BOOK_LIST, novelLists).sendToTarget();
        }
        lastUpdate = model.getLastUpdate(document);
        lastIn = model.getLastIn(document);
    }

    private static class MyHandler extends Handler {

        private WeakReference<BasePActivity> reference;

        public MyHandler(BasePActivity activity) {
            reference = new WeakReference<BasePActivity>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            BasePActivity activity;
            if (reference != null) {
                activity = reference.get();
            }
            switch (msg.what) {
                case HOT_DATA:
                    ((NovelHomeFragment)fragment).setHotView((List<BiquHomeHotEntity>)msg.obj);
                    break;
                case CLASSFIY_DATA:
                    ((NovelHomeFragment)fragment).setClassfiyView((ClassfiyBookEntity) msg.obj);
                    break;
                case BOOK_LIST:
                    ((NovelHomeFragment)fragment).setBookList((List<NovelListEntity>) msg.obj);
                    break;
            }
        }
    }
}
